﻿namespace hedefgrup.graphicsLibrary
{
    using System;
    using System.Drawing;

    public class dispersionGraphic : graphic
    {
        private const float BOTTOM = 411f;
        private const float HEIGHT = 359f;
        private const float LEFT = 91f;
        private const int MAP_NAME_TOP = 14;
        private const float RIGHT = 849f;
        private const float TOP = 52f;
        private const float WIDTH = 758f;

        private void drawAxisStepLinesAndValues(bool isX, float average, Graphics mapArea)
        {
            int num = isX ? ((base.xMaximumValue - base.xMinimumValue) / base.xInterval) : ((base.yMaximumValue - base.yMinimumValue) / base.yInterval);
            float num2 = (isX ? 758f : 359f) / ((float) num);
            int num3 = 0;
            float num4 = isX ? 91f : 411f;
            for (float i = 0f; i <= num; i++)
            {
                if (isX)
                {
                    mapArea.DrawLine(Pens.Black, num4 + (num2 * i), average, num4 + (num2 * i), average + 3f);
                    mapArea.DrawString(num3.ToString() + "%", base.xLabelFont, Brushes.Black, (float) ((num4 + (num2 * i)) - 5f), (float) (average + 4f));
                }
                else
                {
                    mapArea.DrawLine(Pens.Black, average - 3f, num4 - (num2 * i), average, num4 - (num2 * i));
                    mapArea.DrawString(num3.ToString() + "%", base.yLabelFont, Brushes.Black, (float) (average - 26f), (float) ((num4 - (num2 * i)) - 5f));
                }
                num3 += Convert.ToInt32(isX ? base.xInterval : base.yInterval);
            }
        }

        private static float findAverage(float[] values)
        {
            float num = 0f;
            foreach (float num2 in values)
            {
                num += num2;
            }
            return (num / ((float) values.Length));
        }

        public override Bitmap generateImage()
        {
            int num;
            Bitmap image = new Bitmap(templates.dispersionGraphicTemplate);
            Graphics mapArea = Graphics.FromImage(image);
            for (num = 0; num < base.mapNameLines.Length; num++)
            {
                mapArea.DrawString(base.mapNameLines[num], base.mapNameFont, Brushes.Black, new PointF((916f - mapArea.MeasureString(base.mapNameLines[num], base.mapNameFont).Width) / 2f, (float) (14 + (num * 20))));
            }
            float average = 91f + (758f * (findAverage(base.xValues) / ((float) (base.xMaximumValue - base.xMinimumValue))));
            float num3 = 411f - (359f * (findAverage(base.yValues) / ((float) (base.yMaximumValue - base.yMinimumValue))));
            mapArea.DrawLine(Pens.Gray, 91f, num3, 849f, num3);
            mapArea.DrawLine(Pens.Gray, average, 52f, average, 411f);
            this.drawAxisStepLinesAndValues(true, num3, mapArea);
            this.drawAxisStepLinesAndValues(false, average, mapArea);
            for (num = 0; num < base.xValues.Length; num++)
            {
                float num4 = 91f + (758f * (base.xValues[num] / ((float) (base.xMaximumValue - base.xMinimumValue))));
                float num5 = 411f - (359f * (base.yValues[num] / ((float) (base.yMaximumValue - base.yMinimumValue))));
                mapArea.FillRectangle(Brushes.DarkBlue, (float) (num4 - 2f), (float) (num5 - 2f), (float) 5f, (float) 5f);
            }
            mapArea.Dispose();
            return image;
        }
    }
}

